/************************************************************
 * Project Name		[Thinking_In_Java]
 * File Name     	[Example_03_Wrap_Unchecked_Exception.java]
 * Creation Date 	[28-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_07_generics;

import java.io.File;
import java.util.Scanner;

/**
 * 利用泛型封装unchecked exception
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Example_03_Wrap_Unchecked_Exception {

	/**
	 * 测试方法
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		new WrapperException() {
			@Override
			public void doSomething() throws Exception {
				@SuppressWarnings("resource")
				Scanner in = new Scanner(new File("Test.txt"));// 不需要捕获FileNotFoundException
				while (in.hasNext()) {
					System.out.println(in.nextLine());
				}
			}
		}.getThread().start();
	}
}

/**
 * 
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
abstract class WrapperException {

	public abstract void doSomething() throws Exception;

	@SuppressWarnings("unchecked")
	public static <T extends Throwable> void throwAs(Throwable e) throws T {
		throw (T) e;
	}

	public Thread getThread() {
		return new Thread() {
			public void run() {
				try {
					doSomething();
				} catch (Throwable t) {
					WrapperException.<RuntimeException> throwAs(t);
				}
			}
		};

	}

}